<?php


namespace app\api\controller\v28;


use app\api\controller\v2\Auth;
use app\common\cachemodel\AppConfig;
use app\common\cachemodel\ZlFortuneSharePhoto;
use app\common\cachemodel\ZlPosterShareActive;
use think\Env;

class Fortune extends Auth
{
    private $reviveDayName = [
        '破壳之日' => 'love',
        '迈进之日' => 'love',
        '欢聚之日' => 'love',
        '纳新之日' => 'love',
        '桃花之日' => 'love',
        '幸福之日' => 'love',
        '热情之日' => 'love',
        '欢喜之日' => 'love',
        '觉醒之日' => 'harvest',
        '转运之日' => 'harvest',
        '开拓之日' => 'harvest',
        '成果之日' => 'harvest',
        '跨越之日' => 'harvest',
        '蜕变之日' => 'busy',
        '奔波之日' => 'busy',
        '打破之日' => 'busy',
        '燃脂之日' => 'busy',
        '运动之日' => 'busy',
        '用脑之日' => 'busy',
        '养生之日' => 'distressed',
        '冲突之日' => 'distressed',
        '迷茫之日' => 'distressed',
        '独处之日' => 'distressed',
        '不和之日' => 'suffering',
        '低谷之日' => 'suffering',
        '扎心之日' => 'suffering',
        '破财之日' => 'suffering',
        '受伤之日' => 'suffering',
        '慎重之日' => 'suffering',
        '许愿之日' => 'pray',
        '重启之日' => 'pray',
        '闲暇之日' => 'power',
        '静心之日' => 'power',
        '忙碌之日' => 'power',
        '社交之日' => 'power',
        '复杂之日' => 'power',
        '警惕之日' => 'power',
        '低落之日' => 'power',
    ];

    public function getPosterResource()
    {
        $day = input('name/s', '');

        if (!isset($this->reviveDayName[$day]))
        {
            return show(config('code.error'), '请求异常');
        }

        $fortuneSharePhotoModel = new ZlFortuneSharePhoto();

        $stype1ImgData = $fortuneSharePhotoModel->getSharePhoto(1);
        $stype3ImgData = $fortuneSharePhotoModel->getSharePhoto(2);

        $fortuneShareConfig = (new AppConfig())->configInfo('fortune_share');

        $switch = (int)($fortuneShareConfig['poster_share_page_active_show_switch'] ?? 0);

        $posterActiveData = null;
        if ($switch)
        {
            $posterShareActiveModel = new ZlPosterShareActive();
            $weekNum = $posterShareActiveModel->getActiveWeekNum();
            $posterId = $posterShareActiveModel->getNowWeekPosterId($this->user['id'], $weekNum);
            $status = $posterId ? 1 : 0;
            $isNewZan = 0;
            if ($posterId)
            {
                $posterInfo = $posterShareActiveModel->getInfoById($posterId, ['status','old_zan','zan']);
                if ($posterInfo)
                {
                    if (isset($posterInfo['status']) && (int)$posterInfo['status'] === 2)
                    {
                        $status = 2;
                    } else
                    {
                        $oldZan = $posterInfo['old_zan'] ?? 0;
                        $isNewZan = $posterInfo['zan'] > $oldZan ? 1 : 0;
                    }
                }
            }

            $posterActiveData = [
                'target' => 'link',
                'link' => Env::get('host.domain') . '/web/active/fortune_share.html?need_auth=2&zhunle_not_show_nav=1&zhunle_nav_text_color=1',
                'title' => '海报评选',
                'btn_name' => '参加评选',
                'is_new_zan' => $isNewZan,
                'is_join' => $status  // 1=>已参与。 0=>未参与  2=>违规本期禁止参与
            ];
        }

        $data = [
            'revive_day_icon' => '/test/fortune/poster_resource/' . $this->reviveDayName[$day] . '_icon.png',
            'style1_data' => [
                'is_qrcode' => (int)($fortuneShareConfig['style1_is_qrcode'] ?? 0),
                'imgs' => array_column($stype1ImgData, 'url'),
                'is_custom' => 1
            ],
            'style2_data' => [
                'is_qrcode' => (int)($fortuneShareConfig['style2_is_qrcode'] ?? 0),
                'style1_img' => '/test/fortune/poster_resource/' . $this->reviveDayName[$day] . '1.png',
                'style2_img' => '/test/fortune/poster_resource/' . $this->reviveDayName[$day] . '2.png',
            ],
            'style3_data' => [
                'is_qrcode' => (int)($fortuneShareConfig['style3_is_qrcode'] ?? 0),
                'imgs' => array_column($stype3ImgData, 'url'),
                'is_custom' => 1
            ],
            'poster_active_switch' => $switch,
            'poster_active_data' => $posterActiveData
        ];

        return show(config('code.success'), '请求成功', $data);
    }
}
